package com.ruoyi.web.controller.hh;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.HhOrder;
import com.ruoyi.domain.HhUserAccount;
import com.ruoyi.dto.OrderPayDto;
import com.ruoyi.service.IHhOrderService;
import com.ruoyi.utils.ParamsDictUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 结算单、退货单Controller
 * @author chongjin
 * @date 2020-11-18
 */
@Controller
public class HhCloseOrderController extends BaseController {
    private String prefix = "hh/saleManage/close";

    @Autowired
    private IHhOrderService hhOrderService;

    @GetMapping("/close/manage")
    public String manage() {
        return prefix + "/close_manage";
    }

    /**
     * 查询结算单列表
     */
    @PostMapping("/close/manage/list")
    @ResponseBody
    public TableDataInfo list(HhOrder hhOrder) {
        startPage();
        hhOrder.setOrderType(ParamsDictUtil.ORDER_TYPE_CLOSE);
        List<HhOrder> list = hhOrderService.selectHhOrderList(hhOrder);
        return getDataTable(list);
    }

    /**
     * 查询销售单列表
     */
    @PostMapping("/close/manage/saleList")
    @ResponseBody
    public TableDataInfo saleList(HhOrder hhOrder) {
        startPage();
        List<HhOrder> list = hhOrderService.selectAllSaleOrder(hhOrder);
        return getDataTable(list);
    }

    /**
     * 查询退货单列表
     */
    @PostMapping("/close/manage/returnList")
    @ResponseBody
    public TableDataInfo returnList(HhOrder hhOrder) {
        startPage();
        List<HhOrder> list = hhOrderService.selectAllReturnOrder(hhOrder);
        return getDataTable(list);
    }

    /**
     * 查询付款列表
     */
    @PostMapping("/close/manage/accountList")
    @ResponseBody
    public TableDataInfo accountList(HhOrder hhOrder) {
        startPage();
        hhOrder.setOrderType(ParamsDictUtil.ORDER_TYPE_CLOSE);
        List<HhUserAccount> list = hhOrderService.selectAllPayAccount(hhOrder);
        return getDataTable(list);
    }

    /**
     * 新增结算单
     */
    @Log(title = "退货结算", businessType = BusinessType.INSERT)
    @PostMapping("/close/manage/closeReturn")
    @ResponseBody
    public AjaxResult closeReturn(OrderPayDto orderPayDto) {
        hhOrderService.createCloseHhOrder(orderPayDto);
        return AjaxResult.success();
    }

    /**
     * 查看结算单详情
     */
    @GetMapping("/close/manage/detail/{orderId}")
    public String closeDetail(@PathVariable("orderId") String orderId, ModelMap mmap) {
        HhOrder hhOrder = hhOrderService.getCloseInfo(orderId);
        mmap.put("hhOrder", hhOrder);
        return prefix + "/close_detail";
    }

    /**
     * 获取结算单付款页面
     */
    @GetMapping("/close/manage/pay/{orderId}")
    public String getPayInfo(@PathVariable("orderId") String orderId, ModelMap mmap) {
        HhOrder hhOrder = hhOrderService.getCloseInfo(orderId);
        mmap.put("hhOrder", hhOrder);
        return prefix + "/close_pay";
    }

    /**
     * 结算单付款
     */
    @Log(title = "结算付款", businessType = BusinessType.UPDATE)
    @PostMapping("/close/manage/pay")
    @ResponseBody
    public AjaxResult orderPay(OrderPayDto orderPayDto) {
        return toAjax(hhOrderService.orderPay(orderPayDto));
    }

    /**
     * 获取结算单结清页面
     */
    @GetMapping("/close/manage/clear/{orderId}")
    public String getClearInfo(@PathVariable("orderId") String orderId, ModelMap mmap) {
        HhOrder hhOrder = hhOrderService.getCloseInfo(orderId);
        mmap.put("hhOrder", hhOrder);
        return prefix + "/close_clear";
    }

    /**
     * 结算单师傅结清
     */
    @Log(title = "师傅结清", businessType = BusinessType.UPDATE)
    @PostMapping("/close/manage/clear")
    @ResponseBody
    public AjaxResult orderClear(String orderId) {
        return toAjax(hhOrderService.orderWorkerClear(orderId));
    }

}
